package org.adorsys.aderp.orga.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.adorsys.aderp.orga.domain.EmailAddressRole;
import org.adorsys.aderp.orga.domain.OrgaPerson;
import org.adorsys.aderp.orga.domain.OrgaResource;
import org.adorsys.aderp.orga.domain.PersonEmail;
import org.adorsys.aderp.orga.domain.ResourceEmail;
import org.adorsys.aderp.orga.service.EmailAddressRoleService;
import org.adorsys.aderp.orga.service.OrgaResourceService;
import org.adorsys.aderp.orga.service.ResourceEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/resourceemails")
@Controller
@RooWebScaffold(path = "resourceemails", formBackingObject = ResourceEmail.class)
public class ResourceEmailController {
	  
	@RequestMapping(params = "form", produces = "text/html")
    public String createForm(@RequestParam(value="orgaResourceId", required=false)Long orgaResourceId,
    		@RequestParam(value="addressRoleId", required=false)Long addressRoleId,
    		Model uiModel) 
    {
    	populateCreateForm(uiModel, new ResourceEmail(), orgaResourceId, addressRoleId);
        List<String[]> dependencies = new ArrayList<String[]>();
        if (orgaResourceService.countAllOrgaResources() == 0) {
            dependencies.add(new String[] { "orgaresource", "orgaresources" });
        }
        if (emailAddressRoleService.countAllEmailAddressRoles() == 0) {
            dependencies.add(new String[] { "emailaddressrole", "emailaddressroles" });
        }
        uiModel.addAttribute("dependencies", dependencies);
        return "resourceemails/create";
    }

    
    @RequestMapping(produces = "text/html")
    public String list(@RequestParam(value="orgaResourceId", required=false)Long orgaResourceId,
    		@RequestParam(value="addressRoleId", required=false)Long addressRoleId,
    		@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {

		OrgaResource orgaResource = orgaResourceId!=null? orgaResourceService.findOrgaResource(orgaResourceId):null;
		EmailAddressRole addressRole = addressRoleId!=null?emailAddressRoleService.findEmailAddressRole(addressRoleId):null;
		
		 Page<ResourceEmail> resourceEmailPage = resourceEmailService.searchResourceEmail(orgaResource, addressRole, page, size);
		uiModel.addAttribute("resourceEmails", resourceEmailPage.getContent());
		uiModel.addAttribute("maxPages", resourceEmailPage.getNumberOfElements());

        addDateTimeFormatPatterns(uiModel);
        return "resourceemails/list";
    }
    
    void populateCreateForm(Model uiModel, ResourceEmail resourceEmail, Long orgaResourceId, Long addressRoleId) {
		OrgaResource orgaResource = orgaResourceId!=null? orgaResourceService.findOrgaResource(orgaResourceId):null;
		EmailAddressRole emailAddressRole = addressRoleId!=null?emailAddressRoleService.findEmailAddressRole(addressRoleId):null;
		resourceEmail.setOrgaResource(orgaResource);
		resourceEmail.setAddressRole(emailAddressRole);
        uiModel.addAttribute("resourceEmail", resourceEmail);
        addDateTimeFormatPatterns(uiModel);
        
        if(emailAddressRole!=null){
        	uiModel.addAttribute("emailaddressroles", Collections.singleton(emailAddressRole));
        } else {
        	uiModel.addAttribute("emailaddressroles", emailAddressRoleService.findAllEmailAddressRoles());
        }
        if(orgaResource!=null){
        	uiModel.addAttribute("orgaresources", Collections.singleton(orgaResource));
        } else {
        	uiModel.addAttribute("orgaresources", orgaResourceService.findAllOrgaResources());
        }
    }
}
